<template>
  <!-- 收货 -->
  <el-dialog
    :visible.sync="show"
    :before-close="cancel"
    title="收货数量"
    width="700px"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <div>
      <el-form ref="receiveGoodsNumRef" :model="form" :rules="rules" size="small" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('采购数量')" prop="type">
              <el-input disabled v-model="form.num" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 只能加不能减，最小值就是回显的值 -->
            <el-form-item :label="$t('收货数量')" prop="receiveNum">
              <el-input-number
                style="width: 100%"
                v-model="form.receiveNum"
                :precision="0"
                controls-position="right"
                :min="0"
                :max="form.num"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退款数量" prop="refundNum">
              <el-input v-model="form.refundNum" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额" prop="refundPrice">
              <el-input
                type="number"
                style="width: 100%"
                placeholder="请输入"
                v-model="form.refundPrice"
                class="input-with-select"
              >
                <!-- 付款总价单位 1-RMB 2-JPY 3-USD -->
                <el-button slot="append">
                  <el-select style="width: 100px" v-model="form.refundPriceUnit">
                    <el-option label="RMB" :value="1" />
                    <el-option label="JPY" :value="2" />
                    <el-option label="USD" :value="3" />
                  </el-select>
                </el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退款原因">
              <el-input v-model="form.refundReason" :rows="4" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button
        type="primary"
        v-hasPermi="['distribution:skuConsumablePurchase:receiveConsumable']"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { receiveConsumable } from '@/api/distribution/sku/skuConsumablePurchase'
import { getConversionRateList } from '@/api/purchasingManage/manufacturerPurchaseOrder'
import { useDict } from '@/utils/useDict'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      required: true,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**最小值 收货数量 */
      receiveNumMin: null,
      typeList: [],
      form: {},
      rules: {
        receiveNum: [{ required: true, message: '请输入', trigger: 'blur' }],
        refundNum: [{ required: true, message: '请输入', trigger: 'blur' }],
        refundPrice: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.form = JSON.parse(JSON.stringify(this.formData))
        this.receiveNumMin = this.formData.receiveNum
        this.form.refundNum = this.addZero(this.form.refundNum)
        this.form.refundPrice = this.addZero(this.form.refundPrice)

        this.form.refundPriceUnit = this.form.refundPriceUnit ? this.form.refundPriceUnit : 1
      }
    },
  },
  methods: {
    addZero(value) {
      if (!value) return 0
      return value
    },
    handleSubmit() {
      this.$refs['receiveGoodsNumRef'].validate((valid) => {
        if (valid) {
          receiveConsumable(this.form).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.cancel()
              this.$emit('success')
            }
          })
        }
      })
    },
    /**
     * 获取采购类型
     */
    getTypeList() {
      useDict('purchase_type').then((res) => {
        const { purchase_type } = res
        this.typeList = purchase_type
      })
    },

    setSkuUnitPrice(num) {
      if (this.form.skuUnitPrice) this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed(4)
    },
    setInfo(e) {
      this.form.skuTotalValue = (+this.form.payPrice * +this.exchangeRate).toFixed(4)
      this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed(4)
    },
    /**获取汇率 */
    getExchangeRate() {
      getConversionRateList().then((res) => {
        this.exchangeRateList = res.data
        const cnRate = res.data?.filter((i) => i.baseCode == 'CNY')
        this.exchangeRate = cnRate[0].conversionRate
      })
    },
    /**设置汇率 */
    handleExchangeRate(id) {
      // 付款总价单位 1-RMB 2-JPY 3-USD
      if (id == 1) {
        const cnRate = this.exchangeRateList?.filter((i) => i.baseCode == 'CNY')
        this.exchangeRate = cnRate[0].conversionRate
      }
      if (id == 2) {
        this.exchangeRate = 1
      }
      if (id == 3) {
        const cnRate = this.exchangeRateList?.filter((i) => i.baseCode == 'USD')
        this.exchangeRate = cnRate[0].conversionRate
      }

      // 重新计算  总货值  单价
      this.form.skuTotalValue = (+this.form.payPrice * +this.exchangeRate).toFixed(4)
      this.form.skuUnitPrice = (this.form.skuTotalValue / this.form.num).toFixed(4)
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep .el-descriptions-item__cell {
  width: 200px !important;
}
</style>
